# Copyright (c) 2007, Enrico Franchi
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the University of California, Berkeley nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


from Foundation import *
from AppKit import *

from PyObjCTools import NibClassBuilder

class AppLogger(NibClassBuilder.AutoBaseClass):
    '''AppLogger is the preferred way for logging in iport.
    
    AppLogger objects are meant to be obtained through the class method
    sharedLogger().
    
    In fact we have an AppLogger instance in the main .nib file. 
    Since it is the first instance created, it is the one that is always obtained
    through sharedLogger. In fact such instance has also an outlet to a TextView
    area in the LogPanel, so everything that is logged may be seen directly from
    the application.
    
    Early in the initialization process, we also substitute sys.stdout with that
    instance of AppLogger. Everything we 'print' with the Python statement is
    logged both in the standard log files and displayed on the logging TextView
    inside the application.
    
    An AppLogger has the methods
    
    log_(message)
    write(message)
    
    that do the very same thing. In the future we may want to create different
    methods, such as 'info', 'error' and such, like in the 'logging' Python module
    '''
    
    _instance = None
        
    def init(self):
        if AppLogger._instance:
            NSLog('Returning old logger')
            return AppLogger._instance
        else:
            NSLog('*** Creating new logger')
            AppLogger._instance = self
            return self
    
    @classmethod        
    def sharedLogger(self):
        if AppLogger._instance:
            return AppLogger._instance
        else:
            return AppLogger.alloc().init()
            
    def log_(self, message):
        NSLog(message.strip())
        if self.logTextView:
            if message.startswith('Called'):
                color = NSColor.redColor()
            else:     
                color = NSColor.blackColor()
            message = NSAttributedString.alloc().initWithString_attributes_(
                    message, {NSForegroundColorAttributeName:color}
            )
            self.logTextView.setEditable_(True)
            ts = self.logTextView.textStorage()
            ts.insertAttributedString_atIndex_(
                message, len(ts)
            
            )
            self.logTextView.setEditable_(False)
            
    def write(self, message):
        self.log_(message)
